﻿Public Class DOSelection

    Private m_tableName As String
    Private m_table As DataObject
    Private m_columnName As String
    Private m_selectAs As String = ""
    Private m_function As DOFunction = DOFunction.None_

    Public Sub New(ByVal table As DataObject, ByVal column As [Enum])
        Me.m_table = table
        Me.m_tableName = table.TableName.ToString()
        Me.m_columnName = column.ToString()
    End Sub

    Public Sub New(ByVal table As DataObject, ByVal column As [Enum], ByVal selectAs As String)
        Me.m_table = table
        Me.m_tableName = table.TableName.ToString()
        Me.m_columnName = column.ToString()
        Me.m_selectAs = selectAs
    End Sub

    Public Sub New(ByVal tableName As String, ByVal column As [Enum], ByVal selectAs As String)
        Me.m_table = Table
        Me.m_tableName = tableName.ToString()
        Me.m_columnName = column.ToString()
        Me.m_selectAs = selectAs
    End Sub

    Public Sub New(ByVal aFunction As DOFunction, ByVal table As DataObject, ByVal column As [Enum])
        Me.m_table = table
        Me.m_function = aFunction
        Me.m_tableName = table.TableName.ToString()
        Me.m_columnName = column.ToString()
    End Sub

    Public Sub New(ByVal aFunction As DOFunction, ByVal table As DataObject, ByVal column As [Enum], ByVal selectAs As String)
        Me.m_table = table
        Me.m_tableName = table.TableName.ToString()
        Me.m_columnName = column.ToString()
        Me.m_selectAs = selectAs
        Me.m_function = aFunction
    End Sub


    ''' <summary>
    ''' Create a SELECT * for the specified table
    ''' </summary>
    ''' <param name="table"></param>
    ''' <remarks></remarks>
    Public Sub New(ByVal table As DataObject)
        Me.m_table = table
        Me.m_tableName = table.TableName.ToString()
        Me.m_columnName = "*"
    End Sub

    Friend Sub New(ByVal tableName As String, ByVal column As String)
        Me.m_tableName = tableName
        Me.m_columnName = column
    End Sub

    Friend Sub New(ByVal tableName As String)
        Me.m_tableName = tableName.ToString()
        Me.m_columnName = "*"
    End Sub

#Region "Properties"

    Public ReadOnly Property TableName() As String
        Get
            Return Me.m_tableName
        End Get
    End Property

    Public ReadOnly Property Table() As DataObject
        Get
            Return Me.m_table
        End Get
    End Property

    Public ReadOnly Property ColumnName() As String
        Get
            Return Me.m_columnName
        End Get
    End Property

    Public ReadOnly Property SelectAs() As String
        Get
            Return Me.m_selectAs
        End Get
    End Property

    Public ReadOnly Property IsAllColumns() As Boolean
        Get
            If Me.m_columnName = "*" Then
                Return True
            Else
                Return False
            End If
        End Get
    End Property

    Public ReadOnly Property FunctionName() As DOFunction
        Get
            Return Me.m_function
        End Get
    End Property

#End Region




#Region "Render column for SQLBuilder"

    Friend Function Render(ByVal addSelectAsAlias As Boolean) As String
        Dim startStr As String = ""
        Dim endStr As String = ""
        If Me.FunctionName <> DOFunction.None_ Then
            startStr = Me.FunctionName.ToString() & "("
            endStr = ")"
        End If
        Dim str As String = startStr
        str &= Me.TableName & "." & Me.ColumnName & endStr
        If addSelectAsAlias Then
            str &= IIf(Me.SelectAs <> "", " AS " & Me.SelectAs, "")
        End If
        Return str
    End Function

    Friend Function GetResultColumnName() As String
        If Me.SelectAs <> "" Then
            Return Me.SelectAs
        Else
            Return Me.Render(False)
        End If
    End Function



#End Region

End Class
